package com.meichis.yndms.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.meichis.mcsappframework.adapter.recyclerview.MultiItemTypeAdapter;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.VstFreeQuestionAdapter;
import com.meichis.yndms.common.MCode;
import com.meichis.yndms.databinding.ActivityVstFreequestionnaireBinding;
import com.meichis.yndms.model.InspectTemplate;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.VSTFreeQuestionnaireVm;

import java.util.ArrayList;


/**
 * @author : create by  szh
 * @date : 2022/11/9 11:25
 * @desc :问卷填写
 */

public class VST_FreeQuestionnaireListActivity extends BaseActivity {

    ActivityVstFreequestionnaireBinding binding;
    private VSTFreeQuestionnaireVm vm;
    private VstFreeQuestionAdapter adapter;

    private String workGuid, code;
    private ArrayList<Integer> questionIds = new ArrayList<>();

    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_vst_freequestionnaire);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(VSTFreeQuestionnaireVm.class);

    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("问卷填写");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());

        workGuid = getIntent().getStringExtra(MCode.VISITGUID);
        code = getIntent().getStringExtra(MCode.CODE);

        adapter = new VstFreeQuestionAdapter(this, R.layout.item_vst_free_question, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);

        vm.GetQuestion();
        initEvent();
        addObserver();
    }

    private void initEvent() {
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                InspectTemplate inspectTemplate = adapter.getDatas().get(position);
                Intent intent = new Intent();
                intent.putExtra(MCode.QUESTION, inspectTemplate);
                intent.putExtra(MCode.VISITGUID, workGuid);
                intent.putExtra(MCode.CODE, code);
                intent.putExtra("questionIds", questionIds);
                if (inspectTemplate.getFillType() == 1) {
                    intent.setClass(VST_FreeQuestionnaireListActivity.this, VST_FreeQuestionnaireOrderActivity.class);
                } else {
                    intent.setClass(VST_FreeQuestionnaireListActivity.this, VST_FreeQuestionnaireLogicActivity.class);
                }
                startActivity(intent);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, Object o, int position) {
                return false;
            }
        });
    }


    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getQuestionList().observe(this, questionList -> {
                    for (InspectTemplate inspectTemplate : questionList) {
                        questionIds.add(inspectTemplate.getID());
                    }
                    adapter.setList(questionList);
                }
        );
    }
}
